package com.cskaoyan.service.wxaddress;

import com.cskaoyan.bean.MallAddress;
import com.cskaoyan.bean.MallAddressExample;
import com.cskaoyan.bean.MallRegion;
import com.cskaoyan.bean.MallRegionExample;
import com.cskaoyan.mapper.MallAddressMapper;
import com.cskaoyan.mapper.MallRegionMapper;
import com.cskaoyan.model.vo.BaseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: mjm
 * @Date: 2021/12/2
 * @Time: 11:05
 * @Description:
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MallAddressMapper mallAddressMapper;

    @Autowired
    MallRegionMapper mallRegionMapper;

    @Override
    public BaseData addressList(Integer uid) {
        MallAddressExample example = new MallAddressExample();
        //逻辑删除的不显示
        example.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(uid);

        List<MallAddress> mallAddresses = mallAddressMapper.selectByExample(example);
        long total = mallAddresses.size();
        return BaseData.list(mallAddresses,total);
    }

    @Override
    public MallAddress addressDetail(Integer id) {

        MallAddress mallAddress = mallAddressMapper.selectByPrimaryKey(id);
        return mallAddress;

    }

    @Override
    public int addressSave(MallAddress mallAddress) {
        //插入userid


        //查找行政区域邮编
        MallRegionExample example = new MallRegionExample();
        example.createCriteria().andNameEqualTo(mallAddress.getProvince());
        List<MallRegion> mallRegions = mallRegionMapper.selectByExample(example);
        MallRegion mallRegion = mallRegions.get(0);


        //设置行政区域邮编
        mallAddress.setAreaCode(Integer.toString(mallRegion.getCode()));
        mallAddress.setPostalCode(Integer.toString(mallRegion.getCode()));
        if(mallAddress.getAddTime() == null){
            mallAddress.setAddTime(new Date());
        }
        mallAddress.setUpdateTime(new Date());
        mallAddress.setDeleted(false);

        //新建
        if(mallAddress.getId() == 0){
            mallAddress.setId(null);
            //设置了默认地址，判断是否存在默认地址
            if(mallAddress.getIsDefault()){
                MallAddressExample example1 = new MallAddressExample();
                example1.createCriteria().andIsDefaultEqualTo(true);
                List<MallAddress> mallAddresses = mallAddressMapper.selectByExample(example1);
                //没有设置默认地址
                if(mallAddresses.size() != 0){
                    //设置了默认地址，修改默认地址
                    MallAddress mallAddress2 = mallAddresses.get(0);
                    mallAddress2.setIsDefault(false);
                    int i = mallAddressMapper.updateByPrimaryKey(mallAddress2);
                    //return i
                }
            }
            //修改
        } else {
            //设置为默认地址
            if(mallAddress.getIsDefault()){
                MallAddressExample example1 = new MallAddressExample();
                example1.createCriteria().andIsDefaultEqualTo(true);
                List<MallAddress> mallAddresses = mallAddressMapper.selectByExample(example1);
                //没有设置默认地址
                if(mallAddresses.size() == 0){
                    return mallAddressMapper.updateByPrimaryKeySelective(mallAddress);
                }
                //设置了默认地址，修改默认地址
                MallAddress mallAddress2 = mallAddresses.get(0);
                mallAddress2.setIsDefault(false);
                int i = mallAddressMapper.updateByPrimaryKey(mallAddress2);
                //return i;
            }
            //不设置为默认地址
            return mallAddressMapper.updateByPrimaryKeySelective(mallAddress);
        }

        int affectedRows = mallAddressMapper.insertSelective(mallAddress);
        return affectedRows;
    }

    @Override
    public int addressDelete(Integer id) {
        MallAddressExample example = new MallAddressExample();
        example.createCriteria().andIdEqualTo(id);
        //逻辑删除
        MallAddress mallAddress = new MallAddress();
        mallAddress.setId(id);
        mallAddress.setAddTime(new Date());
        mallAddress.setUpdateTime(new Date());
        mallAddress.setDeleted(true);
        int affectedRows = mallAddressMapper.updateByExampleSelective(mallAddress,example);
        return affectedRows;
    }
}
